#include "VKBuffer.h"
#include "VKContext.h"
#include "VKTools.h"
VKBuffer::VKBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
{
	m_BufferSize = size;
	VkBufferCreateInfo bufferInfo = {};
	bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
	bufferInfo.size = size;
	bufferInfo.usage = usage;
	bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

	if (vkCreateBuffer(VKContext::m_VKDevice->m_LogicDevice, &bufferInfo, nullptr, &m_Buffer) != VK_SUCCESS)
		SDL_Log("failed to create buffer!");

	VkMemoryRequirements memRequiredMents;
	vkGetBufferMemoryRequirements(VKContext::m_VKDevice->m_LogicDevice, m_Buffer, &memRequiredMents);

	VkMemoryAllocateInfo allocInfo = {};
	allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
	allocInfo.allocationSize = memRequiredMents.size;
	allocInfo.memoryTypeIndex = VKTools::FindMemoryTypeIndex(memRequiredMents.memoryTypeBits, properties);

	if (vkAllocateMemory(VKContext::m_VKDevice->m_LogicDevice, &allocInfo, nullptr, &m_BufferMemory) != VK_SUCCESS)
		SDL_Log("failed to allocate buffer memory!");

	vkBindBufferMemory(VKContext::m_VKDevice->m_LogicDevice, m_Buffer, m_BufferMemory, 0);
}

VKBuffer::~VKBuffer()
{
	vkDestroyBuffer(VKContext::m_VKDevice->m_LogicDevice,m_Buffer,nullptr);
	vkFreeMemory(VKContext::m_VKDevice->m_LogicDevice, m_BufferMemory, nullptr);
}
